import os.path
import random
from fake_useragent import UserAgent


proxies = [
    {'http':'socks5://127.0.0.1:1080'},
    {'https':'socks5://127.0.0.1:1080'}
]

#"""
# 随机生成User-Agent
def get_random_ua():
    # 创建User-Agent对象
    ua = UserAgent()
    useragent = ua.random
    return useragent
#"""


# 随机生产浏览器标识user-agent
def get_ua():
    first_num = random.randint(55, 76)
    third_num = random.randint(0, 3800)
    fourth_num = random.randint(0, 140)
    os_type = ['(Windows NT 6.1;WOW64)', '(Windows NT 10.0;WOW64)', '(X11;Linux x86_64)', '(Macintosh;Intel Mac OS X 10_14_5)']
    chrome_version = 'Chrome/{}.0.{}.{}'.format(first_num, third_num, fourth_num)
    ua = ''.join(["Mozilla/5.0 ", random.choice(os_type), ' AppleWebKit/537.36 ', '(KHTML,like Gecko) ', chrome_version, ' safari/537.36 ', 'Edg/100.0.1185.50'])
    return ua

# head
headers = {
    #'User-Agent': get_random_ua()
}

# 把爬下来的ip地址存到文件中，调用储存在文件中的IP地址，转换为字典形式
def get_ip():
    proxies = {}
    with open('ip池.txt', 'r') as r:
        datas = r.readlines()
        print("txt", datas)
        # print(random.choice(datas))
        proxies["http"] = random.choice(datas).strip("\n")
        # 利用random随机数，随机提取IP
        #data = datas[random.randint(0, 11)]
        #key = data[:6].strip("'")
        #value = data[7:].strip("'").strip('\n').rstrip("'")
        ## 转换为字典
        #proxies[key] = value
        return proxies

if __name__ == '__main__':
    proxies = random.choice(proxies)
    print(proxies)
    print(random.randint(0, 11))
    print(random.random()*3)
    print(random.random()*5)

    print(get_random_ua())

    # print(headers)
    print(get_ip())

    file_path = " /Users/hubert/Downloads/视频测试/标准分辨率/2-1920x1080-1.ts"
    print(os.path.basename(file_path))

    test_json = {}
    test_json['key1'] = "value1"
    test_json['key2'] = 123
    print(test_json) # {'key1': 'value1', 'key2': 123}
    print(get_ua())

